<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>selectionmenubutton.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_ui_selectionmenubutton.js.html">selectionmenubutton.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2009 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview A customized MenuButton for selection of items among lists.
<a name="line17"></a> * Menu contains &#39;select all&#39; and &#39;select none&#39; MenuItems for selecting all and
<a name="line18"></a> * no items by default. Other MenuItems can be added by user.
<a name="line19"></a> *
<a name="line20"></a> * The checkbox content fires the action events associated with the &#39;select all&#39;
<a name="line21"></a> * and &#39;select none&#39; menu items.
<a name="line22"></a> *
<a name="line23"></a> * @see ../demos/selectionmenubutton.html
<a name="line24"></a> */
<a name="line25"></a>
<a name="line26"></a>goog.provide(&#39;goog.ui.SelectionMenuButton&#39;);
<a name="line27"></a>goog.provide(&#39;goog.ui.SelectionMenuButton.SelectionState&#39;);
<a name="line28"></a>
<a name="line29"></a>goog.require(&#39;goog.events.EventType&#39;);
<a name="line30"></a>goog.require(&#39;goog.ui.Component.EventType&#39;);
<a name="line31"></a>goog.require(&#39;goog.ui.Menu&#39;);
<a name="line32"></a>goog.require(&#39;goog.ui.MenuButton&#39;);
<a name="line33"></a>goog.require(&#39;goog.ui.MenuItem&#39;);
<a name="line34"></a>
<a name="line35"></a>
<a name="line36"></a>
<a name="line37"></a>/**
<a name="line38"></a> * A selection menu button control.  Extends {@link goog.ui.MenuButton}.
<a name="line39"></a> * Menu contains &#39;select all&#39; and &#39;select none&#39; MenuItems for selecting all and
<a name="line40"></a> * no items by default. Other MenuItems can be added by user.
<a name="line41"></a> *
<a name="line42"></a> * The checkbox content fires the action events associated with the &#39;select all&#39;
<a name="line43"></a> * and &#39;select none&#39; menu items.
<a name="line44"></a> *
<a name="line45"></a> * @param {goog.ui.ButtonRenderer=} opt_renderer Renderer used to render or
<a name="line46"></a> *     decorate the menu button; defaults to {@link goog.ui.MenuButtonRenderer}.
<a name="line47"></a> * @param {goog.ui.MenuItemRenderer=} opt_itemRenderer Optional menu item
<a name="line48"></a> *     renderer.
<a name="line49"></a> * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM hepler, used for
<a name="line50"></a> *     document interaction.
<a name="line51"></a> * @constructor
<a name="line52"></a> * @extends {goog.ui.MenuButton}
<a name="line53"></a> */
<a name="line54"></a>goog.ui.SelectionMenuButton = function(opt_renderer,
<a name="line55"></a>                                       opt_itemRenderer,
<a name="line56"></a>                                       opt_domHelper) {
<a name="line57"></a>  goog.ui.MenuButton.call(this,
<a name="line58"></a>                          null,
<a name="line59"></a>                          null,
<a name="line60"></a>                          opt_renderer,
<a name="line61"></a>                          opt_domHelper);
<a name="line62"></a>  this.initialItemRenderer_ = opt_itemRenderer || null;
<a name="line63"></a>};
<a name="line64"></a>goog.inherits(goog.ui.SelectionMenuButton, goog.ui.MenuButton);
<a name="line65"></a>
<a name="line66"></a>
<a name="line67"></a>/**
<a name="line68"></a> * Constants for menu action types.
<a name="line69"></a> * @enum {number}
<a name="line70"></a> */
<a name="line71"></a>goog.ui.SelectionMenuButton.SelectionState = {
<a name="line72"></a>  ALL: 0,
<a name="line73"></a>  SOME: 1,
<a name="line74"></a>  NONE: 2
<a name="line75"></a>};
<a name="line76"></a>
<a name="line77"></a>
<a name="line78"></a>/**
<a name="line79"></a> * Select button state
<a name="line80"></a> * @type {goog.ui.SelectionMenuButton.SelectionState}
<a name="line81"></a> * @protected
<a name="line82"></a> */
<a name="line83"></a>goog.ui.SelectionMenuButton.prototype.selectionState =
<a name="line84"></a>    goog.ui.SelectionMenuButton.SelectionState.NONE;
<a name="line85"></a>
<a name="line86"></a>
<a name="line87"></a>/**
<a name="line88"></a> * Item renderer used for the first 2 items, &#39;select all&#39; and &#39;select none&#39;.
<a name="line89"></a> * @type {goog.ui.MenuItemRenderer}
<a name="line90"></a> * @private
<a name="line91"></a> */
<a name="line92"></a>goog.ui.SelectionMenuButton.prototype.initialItemRenderer_;
<a name="line93"></a>
<a name="line94"></a>
<a name="line95"></a>/**
<a name="line96"></a> * Enables button and embedded checkbox.
<a name="line97"></a> * @param {boolean} enable Whether to enable or disable the button.
<a name="line98"></a> * @override
<a name="line99"></a> */
<a name="line100"></a>goog.ui.SelectionMenuButton.prototype.setEnabled = function(enable) {
<a name="line101"></a>  goog.base(this, &#39;setEnabled&#39;, enable);
<a name="line102"></a>  this.setCheckboxEnabled(enable);
<a name="line103"></a>};
<a name="line104"></a>
<a name="line105"></a>
<a name="line106"></a>/**
<a name="line107"></a> * Enables the embedded checkbox.
<a name="line108"></a> * @param {boolean} enable Whether to enable or disable the checkbox.
<a name="line109"></a> * @protected
<a name="line110"></a> */
<a name="line111"></a>goog.ui.SelectionMenuButton.prototype.setCheckboxEnabled = function(enable) {
<a name="line112"></a>  this.getCheckboxElement().disabled = !enable;
<a name="line113"></a>};
<a name="line114"></a>
<a name="line115"></a>
<a name="line116"></a>/** @override */
<a name="line117"></a>goog.ui.SelectionMenuButton.prototype.handleMouseDown = function(e) {
<a name="line118"></a>  if (!this.getDomHelper().contains(this.getCheckboxElement(),
<a name="line119"></a>      /** @type {Element} */ (e.target))) {
<a name="line120"></a>    goog.ui.SelectionMenuButton.superClass_.handleMouseDown.call(this, e);
<a name="line121"></a>  }
<a name="line122"></a>};
<a name="line123"></a>
<a name="line124"></a>
<a name="line125"></a>/**
<a name="line126"></a> * Gets the checkbox element. Needed because if decorating html, getContent()
<a name="line127"></a> * may include and comment/text elements in addition to the input element.
<a name="line128"></a> * @return {Element} Checkbox.
<a name="line129"></a> * @protected
<a name="line130"></a> */
<a name="line131"></a>goog.ui.SelectionMenuButton.prototype.getCheckboxElement = function() {
<a name="line132"></a>  var elements = this.getDomHelper().getElementsByTagNameAndClass(
<a name="line133"></a>      &#39;input&#39;,
<a name="line134"></a>      goog.getCssName(&#39;goog-selectionmenubutton-checkbox&#39;),
<a name="line135"></a>      this.getContentElement());
<a name="line136"></a>  return elements[0];
<a name="line137"></a>};
<a name="line138"></a>
<a name="line139"></a>
<a name="line140"></a>/**
<a name="line141"></a> * Checkbox click handler.
<a name="line142"></a> * @param {goog.events.BrowserEvent} e Checkbox click event.
<a name="line143"></a> * @protected
<a name="line144"></a> */
<a name="line145"></a>goog.ui.SelectionMenuButton.prototype.handleCheckboxClick = function(e) {
<a name="line146"></a>  if (this.selectionState == goog.ui.SelectionMenuButton.SelectionState.NONE) {
<a name="line147"></a>    this.setSelectionState(goog.ui.SelectionMenuButton.SelectionState.ALL);
<a name="line148"></a>    if (this.getItemAt(0)) {
<a name="line149"></a>      this.getItemAt(0).dispatchEvent(  // &#39;All&#39; item
<a name="line150"></a>          goog.ui.Component.EventType.ACTION);
<a name="line151"></a>    }
<a name="line152"></a>  } else {
<a name="line153"></a>    this.setSelectionState(goog.ui.SelectionMenuButton.SelectionState.NONE);
<a name="line154"></a>    if (this.getItemAt(1)) {
<a name="line155"></a>      this.getItemAt(1).dispatchEvent(  // &#39;None&#39; item
<a name="line156"></a>          goog.ui.Component.EventType.ACTION);
<a name="line157"></a>    }
<a name="line158"></a>  }
<a name="line159"></a>};
<a name="line160"></a>
<a name="line161"></a>
<a name="line162"></a>/**
<a name="line163"></a> * Menu action handler to update checkbox checked state.
<a name="line164"></a> * @param {goog.events.Event} e Menu action event.
<a name="line165"></a> * @private
<a name="line166"></a> */
<a name="line167"></a>goog.ui.SelectionMenuButton.prototype.handleMenuAction_ = function(e) {
<a name="line168"></a>  if (e.target.getModel() == goog.ui.SelectionMenuButton.SelectionState.ALL) {
<a name="line169"></a>    this.setSelectionState(goog.ui.SelectionMenuButton.SelectionState.ALL);
<a name="line170"></a>  } else {
<a name="line171"></a>    this.setSelectionState(goog.ui.SelectionMenuButton.SelectionState.NONE);
<a name="line172"></a>  }
<a name="line173"></a>};
<a name="line174"></a>
<a name="line175"></a>
<a name="line176"></a>/**
<a name="line177"></a> * Set up events related to the menu items.
<a name="line178"></a> * @private
<a name="line179"></a> */
<a name="line180"></a>goog.ui.SelectionMenuButton.prototype.addMenuEvent_ = function() {
<a name="line181"></a>  if (this.getItemAt(0) &amp;&amp; this.getItemAt(1)) {
<a name="line182"></a>    this.getHandler().listen(this.getMenu(),
<a name="line183"></a>                             goog.ui.Component.EventType.ACTION,
<a name="line184"></a>                             this.handleMenuAction_);
<a name="line185"></a>    this.getItemAt(0).setModel(goog.ui.SelectionMenuButton.SelectionState.ALL);
<a name="line186"></a>    this.getItemAt(1).setModel(goog.ui.SelectionMenuButton.SelectionState.NONE);
<a name="line187"></a>  }
<a name="line188"></a>};
<a name="line189"></a>
<a name="line190"></a>
<a name="line191"></a>/**
<a name="line192"></a> * Set up events related to the checkbox.
<a name="line193"></a> * @protected
<a name="line194"></a> */
<a name="line195"></a>goog.ui.SelectionMenuButton.prototype.addCheckboxEvent = function() {
<a name="line196"></a>  this.getHandler().listen(this.getCheckboxElement(),
<a name="line197"></a>                           goog.events.EventType.CLICK,
<a name="line198"></a>                           this.handleCheckboxClick);
<a name="line199"></a>};
<a name="line200"></a>
<a name="line201"></a>
<a name="line202"></a>/**
<a name="line203"></a> * Adds the checkbox to the button, and adds 2 items to the menu corresponding
<a name="line204"></a> * to &#39;select all&#39; and &#39;select none&#39;.
<a name="line205"></a> * @override
<a name="line206"></a> * @protected
<a name="line207"></a> */
<a name="line208"></a>goog.ui.SelectionMenuButton.prototype.createDom = function() {
<a name="line209"></a>  goog.ui.SelectionMenuButton.superClass_.createDom.call(this);
<a name="line210"></a>
<a name="line211"></a>  this.createCheckbox();
<a name="line212"></a>
<a name="line213"></a>  /** @desc Text for &#39;All&#39; button, used to select all items in a list. */
<a name="line214"></a>  var MSG_SELECTIONMENUITEM_ALL = goog.getMsg(&#39;All&#39;);
<a name="line215"></a>  /** @desc Text for &#39;None&#39; button, used to unselect all items in a list. */
<a name="line216"></a>  var MSG_SELECTIONMENUITEM_NONE = goog.getMsg(&#39;None&#39;);
<a name="line217"></a>
<a name="line218"></a>  var itemAll = new goog.ui.MenuItem(MSG_SELECTIONMENUITEM_ALL,
<a name="line219"></a>                                     null,
<a name="line220"></a>                                     this.getDomHelper(),
<a name="line221"></a>                                     this.initialItemRenderer_);
<a name="line222"></a>  var itemNone = new goog.ui.MenuItem(MSG_SELECTIONMENUITEM_NONE,
<a name="line223"></a>                                      null,
<a name="line224"></a>                                      this.getDomHelper(),
<a name="line225"></a>                                      this.initialItemRenderer_);
<a name="line226"></a>  this.addItem(itemAll);
<a name="line227"></a>  this.addItem(itemNone);
<a name="line228"></a>
<a name="line229"></a>  this.addCheckboxEvent();
<a name="line230"></a>  this.addMenuEvent_();
<a name="line231"></a>};
<a name="line232"></a>
<a name="line233"></a>
<a name="line234"></a>/**
<a name="line235"></a> * Creates and adds the checkbox to the button.
<a name="line236"></a> * @protected
<a name="line237"></a> */
<a name="line238"></a>goog.ui.SelectionMenuButton.prototype.createCheckbox = function() {
<a name="line239"></a>  var checkbox = this.getDomHelper().createElement(&#39;input&#39;);
<a name="line240"></a>  checkbox.type = &#39;checkbox&#39;;
<a name="line241"></a>  checkbox.className = goog.getCssName(&#39;goog-selectionmenubutton-checkbox&#39;);
<a name="line242"></a>  this.setContent(checkbox);
<a name="line243"></a>};
<a name="line244"></a>
<a name="line245"></a>
<a name="line246"></a>/** @override */
<a name="line247"></a>goog.ui.SelectionMenuButton.prototype.decorateInternal = function(element) {
<a name="line248"></a>  goog.ui.SelectionMenuButton.superClass_.decorateInternal.call(this, element);
<a name="line249"></a>  this.addCheckboxEvent();
<a name="line250"></a>  this.addMenuEvent_();
<a name="line251"></a>};
<a name="line252"></a>
<a name="line253"></a>
<a name="line254"></a>/** @override */
<a name="line255"></a>goog.ui.SelectionMenuButton.prototype.setMenu = function(menu) {
<a name="line256"></a>  goog.ui.SelectionMenuButton.superClass_.setMenu.call(this, menu);
<a name="line257"></a>  this.addMenuEvent_();
<a name="line258"></a>};
<a name="line259"></a>
<a name="line260"></a>
<a name="line261"></a>/**
<a name="line262"></a> * Set selection state and update checkbox.
<a name="line263"></a> * @param {goog.ui.SelectionMenuButton.SelectionState} state Selection state.
<a name="line264"></a> */
<a name="line265"></a>goog.ui.SelectionMenuButton.prototype.setSelectionState = function(state) {
<a name="line266"></a>  if (this.selectionState != state) {
<a name="line267"></a>    var checkbox = this.getCheckboxElement();
<a name="line268"></a>    if (state == goog.ui.SelectionMenuButton.SelectionState.ALL) {
<a name="line269"></a>      checkbox.checked = true;
<a name="line270"></a>      goog.style.setOpacity(checkbox, 1);
<a name="line271"></a>    } else if (state == goog.ui.SelectionMenuButton.SelectionState.SOME) {
<a name="line272"></a>      checkbox.checked = true;
<a name="line273"></a>      // TODO(user): Get UX help to style this
<a name="line274"></a>      goog.style.setOpacity(checkbox, 0.5);
<a name="line275"></a>    } else { // NONE
<a name="line276"></a>      checkbox.checked = false;
<a name="line277"></a>      goog.style.setOpacity(checkbox, 1);
<a name="line278"></a>    }
<a name="line279"></a>    this.selectionState = state;
<a name="line280"></a>  }
<a name="line281"></a>};
<a name="line282"></a>
<a name="line283"></a>
<a name="line284"></a>/**
<a name="line285"></a>* Get selection state.
<a name="line286"></a>* @return {goog.ui.SelectionMenuButton.SelectionState} Selection state.
<a name="line287"></a>*/
<a name="line288"></a>goog.ui.SelectionMenuButton.prototype.getSelectionState = function() {
<a name="line289"></a>  return this.selectionState;
<a name="line290"></a>};
<a name="line291"></a>
<a name="line292"></a>
<a name="line293"></a>// Register a decorator factory function for goog.ui.SelectionMenuButton.
<a name="line294"></a>goog.ui.registry.setDecoratorByClassName(
<a name="line295"></a>    goog.getCssName(&#39;goog-selectionmenubutton-button&#39;),
<a name="line296"></a>    function() {
<a name="line297"></a>      return new goog.ui.SelectionMenuButton();
<a name="line298"></a>    });
</pre>


</body>
</html>
